<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="PID" prop="pid">
        <el-input v-model="dataForm.pid" placeholder="PID"></el-input>
      </el-form-item>-->
      <el-form-item label="区域名字" prop="name">
        <el-input v-model="dataForm.name" placeholder="区域名字"></el-input>
      </el-form-item>
      <el-form-item label="上级区域" prop="parentName">
        <el-popover ref="menuListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级区域"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注信息" prop="commit">
        <el-input v-model="dataForm.commit" placeholder="备注信息"></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        pid: "",
        name: "",
        commit: "",
        status: "",
        parentName: "",
      },
      dataRule: {
        pid: [
          {
            required: true,
            message: "上级不能为空",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "区域名字不能为空", trigger: "blur" },
        ],
        commit: [
          { required: true, message: "备注信息不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      menuList: [],
      menuListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$http({
        url: this.$http.adornUrl("/seat/seatarea/list"),
        method: "get",
        params: this.$http.adornParams({
          key: this.dataForm.key,
        }),
      })
        .then(({ data }) => {
          console.log(data);
          this.menuList = treeDataTranslate(data.page, "id", "pid");
        })
        .then(() => {
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(
                  `/seat/seatarea/info/${this.dataForm.id}`
                ),
                method: "get",
                params: this.$http.adornParams(),
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.dataForm.pid = data.seatArea.pid;
                  this.dataForm.name = data.seatArea.name;
                  this.dataForm.commit = data.seatArea.commit;
                  this.dataForm.status = data.seatArea.status;
                  this.menuListTreeSetCurrentNode();
                }
              });
            }
          });
        });
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.pid = data.id;
      this.dataForm.parentName = data.name;
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.pid);
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() ||
        {})["name"];
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/seat/seatarea/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              pid: this.dataForm.pid,
              name: this.dataForm.name,
              commit: this.dataForm.commit,
              status: this.dataForm.status,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
